
import config from '../config/index.js'
// 菜单
import menuController from './menu.js'
// 路由
import { useRouter } from '../router'
// 访问状态
import { store } from '@naturefw/nf-state'

/**
 * 初始化设置
 */
const docInit = async () => {
  // 获取状态
  const {
    projectInfo,
    naviList,
    menuList,
    current
  } = store

  // 获取网站信息和导航信息
  const projectInfos = await getProject()
  
  // 设置版本
  current.ver = projectInfo.ver

  // 设置网站信息
  projectInfo.$patch({
    projectId: projectInfos.projectId, 
    ver: projectInfos.ver, // 版本信息
    title: projectInfos.title,
    description: projectInfos.description
  })

  // 设置导航信息
  naviList.$state = projectInfos.navi

  // 获取菜单信息
  const { getMenu } = menuController()
  // 设置菜单信息
  menuList.$state = await getMenu(projectInfo.ver)

  const router = useRouter() // 路由
  if (router.menuPath.length === 0) { // 没有默认url
    // 设置默认导航项
    for (let i = 0; i < naviList.length; i++) {
      const nn = naviList[i]
      if (nn.link === 'menu') {
        current.naviId = nn.naviId
        break
      }
    }
  }

  if (router.naviId) {
    current.naviId = router.naviId
  }

  return projectInfos

}